$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/goodshelves/getAnalysisList',
        datatype: "json",
        colModel: [
            { label: '排名', name: 'rank', index: 'rank', width: 80 },
			{ label: '货架名称', name: 'shelvesname', index: 'shelvesname', width: 80},
			{ label: '订单量', name: 'orderCount', index: 'orderCount', width: 80},
			{ label: '商品数量', name: 'productCount', index: 'productCount', width: 80 },
			{ label: '订单金额', name: 'orderMoney', index: 'orderMoney', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.orderMoney == null ||rowdata.orderMoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.orderMoney;
                    }
                }},
            { label: '优惠券金额', name: 'couponMoney', index: 'couponMoney', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.couponMoney == null ||rowdata.couponMoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.couponMoney;
                    }
                }},
            { label: '支付金额', name: 'payMoney', index: 'payMoney', width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.payMoney == null ||rowdata.payMoney == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.payMoney;
                    }
                }},
            { label: '平均订单价', name: 'avgOrderPrice',index: 'avgOrderPrice',  width: 80,formatter: function(cellvalue, options, rowdata) {
                    if(rowdata.avgOrderPrice == null ||rowdata.avgOrderPrice == ''){
                        return "￥"+0;
                    }else{
                        return "￥"+rowdata.avgOrderPrice;
                    }
                }}

        ],
		viewrecords: true,
        height: 385,
        rownumbers: true,
        rowNum: -1,
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        jsonReader : {
            root: "list"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    vm.Statistics(null,null,null,null);
});

var vm = new Vue({
	el:'#rrapp',
	data:{

		showList: true,
		title: null,
		transaction: {}
	},
	methods: {
		query: function () {
            var startime = $("#startime").val();
            var endtime = $("#endtime").val();
            vm.reload(startime,endtime);
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.transaction = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
        Statistics: function (startime,endtime) {
            $.ajax({
                type: "POST",
                async:false,
                url: baseURL + "sys/goodshelves/infoStatistics",
                data:{"startime":startime,"endtime":endtime},
                success: function(r){
                	var orderCount = 0;
                	var productCount = 0;
                	var payMoney = 0;
                    var avgOrderPrice = 0;
                	if(r.goodshelves !=null){
                        orderCount = r.goodshelves.orderCount;
                        productCount = r.goodshelves.productCount;
                        payMoney = r.goodshelves.payMoney == null?0:r.goodshelves.payMoney;
                        avgOrderPrice = r.goodshelves.avgOrderPrice == null?0:r.goodshelves.avgOrderPrice;
					}
                    $("#ddl").text(orderCount);
                    $("#spsl").text(productCount);
                    $("#zfje").text("￥"+payMoney);
                    $("#pjddj").text("￥"+avgOrderPrice);
                }
            });
        },
		setDate: function (num) {
            var mydate = new Date();
            var year = mydate.getFullYear(); //获取完整的年份(4位,1970-????)
            var month   = mydate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            if(month<10){
                month = "0"+month;
            }
            var day = mydate.getDate(); //获取当前日(1-31)
            if(day<10){
                day = "0"+day;
            }

            var preDate = new Date(mydate.getTime() - (24*60*60*1000*num));

            if(day == 0){
                preDate = mydate;
			}
            var preyear = preDate.getFullYear(); //获取完整的年份(4位,1970-????)
            var premonth   = preDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            if(premonth<10){
                premonth = "0"+premonth;
            }
            var preday = preDate.getDate(); //获取当前日(1-31)
            if(preday<10){
                preday = "0"+preday;
            }

            $("#startime").val(preyear+"-"+premonth+"-"+preday);
            $("#endtime").val(year+"-"+month+"-"+day);
        },
		saveOrUpdate: function (event) {
			var url = vm.transaction.id == null ? "sys/transaction/save" : "sys/transaction/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.transaction),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/transaction/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/transaction/info/"+id, function(r){
                vm.transaction = r.transaction;
            });
		},
		reload: function (startime,endtime) {
			vm.showList = true;
			$("#jqGrid").jqGrid('setGridParam',{
                postData:{
                    startime:startime,
                    endtime:endtime
				},
                gridComplete:function(){
                    vm.Statistics(startime,endtime);
                }

            }).trigger("reloadGrid");
		}
	}
});